package com.jwdf.uda.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.ExtraBaseQuestion;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemImageTxtQuestionBinding;
import com.jwdf.uda.databinding.ItemOnlyQuestionBinding;
import com.jwdf.uda.databinding.ItemTxtQuestionBinding;
import com.jwdf.uda.databinding.ViewQuestionBottomBinding;
import com.jwdf.uda.databinding.ViewQuestionTopBinding;
import com.jwdf.uda.ui.activity.QuizDetailActivity;
import com.jwdf.uda.ui.activity.ReplyDetaiActivtiy;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by Administrator on 2016/9/8.
 */
public class GenericQuizAdapter extends AbsAdapter<PbBean.PbBaseQuestion> {
    private static final int ITEM_ONLY_QUESTION = 0;
    private static final int ITEM_TEXT = 1;
    private static final int ITEM_IMAGE_TEXT = 2;

    public GenericQuizAdapter(Context context) {
        super(context);
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        switch (itemType) {
            case ITEM_TEXT:
                return new TextHolder(mInflater.inflate(R.layout.item_txt_question, parent, false));
            case ITEM_IMAGE_TEXT:
                return new ImageTextHolder(mInflater.inflate(R.layout.item_image_txt_question, parent, false));
            default:
                return new QuestionHolder(mInflater.inflate(R.layout.item_only_question, parent, false));
        }
    }

    private void startReplyDetailActivity(View v, PbBean.PbUserSimple userInfo, int answerId, int voteCount, ExtraBaseQuestion baseQuestion) {
        boolean isAuthor = TextUtils.equals(userInfo.getUserId(), ConfigUtils.getUserId(v.getContext()));
        ReplyDetaiActivtiy.start(v.getContext(), answerId, isAuthor, voteCount, baseQuestion);
    }

    private void onBindCommonView(View convertView, ViewQuestionTopBinding topLayout, ViewQuestionBottomBinding bottomLayout, final PbBean.PbBaseQuestion item) {
        onBindBaseTopItem(convertView, topLayout, item);
        final PbBean.PbUserSimple userInfo = item.getBestAnswer().getUserInfo();
        ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), bottomLayout.ivHeader);
        bottomLayout.ivDesigner.setVisibility(userInfo.getIsDesigner() ? View.VISIBLE : View.GONE);
        bottomLayout.tvName.setText(StringUtils.filterNullAndTrim(userInfo.getNickname()));
        bottomLayout.tvAdmireNum.setText(String.valueOf(item.getBestAnswer().getLikeCount()));
        bottomLayout.tvAdmireNum.setVisibility(item.getBestAnswer().getLikeCount() > 0 ? View.VISIBLE : View.GONE);
        bottomLayout.ivReplyLevel.setVisibility(item.getBestAnswer().getLikeCount() > 6 ? View.VISIBLE : View.GONE);
        bottomLayout.ivHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startUserInfoActivity(v, userInfo);
            }
        });
        bottomLayout.tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startUserInfoActivity(v, userInfo);
            }
        });
    }

    @SuppressLint("StringFormatMatches")
    private void onBindBaseTopItem(final View convertView, ViewQuestionTopBinding topLayout, final PbBean.PbBaseQuestion item) {
        if (item.hasActionStr() && item.hasActionUserInfo()) {
            PbBean.PbUserSimple userInfo = item.getActionUserInfo();
            ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), topLayout.ivHeader);
            topLayout.ivDesigner.setVisibility(userInfo.getIsDesigner() ? View.VISIBLE : View.GONE);
            topLayout.tvOperateTxt.setText(item.getActionStr());
            topLayout.tvName.setText(userInfo.getNickname());
            topLayout.tvIntro.setVisibility(!TextUtils.isEmpty(StringUtils.filterNullAndTrim(userInfo.getIntroduction())) ? View.VISIBLE : View.GONE);
            topLayout.tvIntro.setText(userInfo.getIntroduction());
            topLayout.llActionLayout.setVisibility(View.VISIBLE);
        } else {
            topLayout.llActionLayout.setVisibility(View.GONE);
        }
        topLayout.tvCount.setText(convertView.getContext().getString(R.string.item_top_count, item.getPv(), item.getConcernCount(), item.getAnswerCount()));
        topLayout.tvTitle.setText(StringUtils.filterNullAndTrim(item.getTitle()));
        topLayout.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isAuthor = TextUtils.equals(item.getUserInfo().getUserId(), ConfigUtils.getUserId(convertView.getContext()));
                QuizDetailActivity.start(v.getContext(), item.getQuestionId(), isAuthor);
            }
        });
        topLayout.ivHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startUserInfoActivity(v, item.getActionUserInfo());
            }
        });

        topLayout.tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startUserInfoActivity(v, item.getActionUserInfo());
            }
        });

        topLayout.tvIntro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startUserInfoActivity(v, item.getActionUserInfo());
            }
        });
    }

    private void startUserInfoActivity(View v, PbBean.PbUserSimple userInfo) {
        UserInfoActivity.start(v.getContext(), userInfo.getUserId(), userInfo.getIsDesigner());
    }

    @Override
    public int getItemViewType(int position) {
        PbBean.PbBaseQuestion bean = getItem(position);
        return !bean.hasBestAnswer() ? ITEM_ONLY_QUESTION : (bean.getBestAnswer().getQuickImageInfoList() == null || bean.getBestAnswer().getQuickImageInfoList().isEmpty() ? ITEM_TEXT : ITEM_IMAGE_TEXT);
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    private class QuestionHolder extends AbsViewHolder<PbBean.PbBaseQuestion> {
        private ItemOnlyQuestionBinding mBinding;

        public QuestionHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(final PbBean.PbBaseQuestion item) {
            onBindBaseTopItem(convertView, mBinding.topLayout, item);
        }
    }

    private class TextHolder extends AbsViewHolder<PbBean.PbBaseQuestion> {
        private ItemTxtQuestionBinding mBinding;

        public TextHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
            convertView.setOnClickListener(this);
        }

        @Override
        protected void onBindView(final PbBean.PbBaseQuestion item) {
            onBindCommonView(convertView, mBinding.topLayout, mBinding.bottomLayout, item);
            mBinding.tvInfo.setText(StringUtils.filterNullAndTrim(item.getBestAnswer().getQuickDescription()));
        }

        @Override
        public void onClick(View v) {
            boolean isAuthor = TextUtils.equals(item.getUserInfo().getUserId(), ConfigUtils.getUserId(convertView.getContext()));
            int questionId = item.getQuestionId();
            startReplyDetailActivity(v, item.getBestAnswer().getUserInfo(), item.getBestAnswer().getAnswerId(), item.getVoteOptionList().size(), new ExtraBaseQuestion(questionId, isAuthor));
        }
    }

    private class ImageTextHolder extends AbsViewHolder<PbBean.PbBaseQuestion> {
        private ItemImageTxtQuestionBinding mBinding;

        public ImageTextHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
            convertView.setOnClickListener(this);
        }

        @Override
        protected void onBindView(final PbBean.PbBaseQuestion item) {
            onBindCommonView(convertView, mBinding.topLayout, mBinding.bottomLayout, item);
            mBinding.tvInfo.setText(StringUtils.filterNullAndTrim(item.getBestAnswer().getQuickDescription()));
            mBinding.tvThumbNum.setText(item.getBestAnswer().getQuickImageCount() + "张");
            ImageLoader.getInstance().displayImage(item.getBestAnswer().getQuickImageInfoList().get(0).getUrl(), mBinding.ivThumb);
        }

        @Override
        public void onClick(View v) {
            boolean isAuthor = TextUtils.equals(item.getUserInfo().getUserId(), ConfigUtils.getUserId(convertView.getContext()));
            int questionId = item.getQuestionId();
            startReplyDetailActivity(v, item.getBestAnswer().getUserInfo(), item.getBestAnswer().getAnswerId(), item.getVoteOptionList().size(), new ExtraBaseQuestion(questionId, isAuthor));
        }
    }

}
